<?php
class  ApiCenter_VendorsApi_Model_Api2_Warehouse  extends  Mage_Api2_Model_Resource {
    private $details_filter=array(
        'sender_phone'=>array("require"),
        'sender_address'=>array('require',"en"),
        'sender_phone'=>array("require","num"),
        'sender_country'=>array("require"),
        'sender_province'=>array("require"),
        'sender_city'=>array("require"),
        'sender_company'=>array("require","en"),
        'sender_email'=>array("require"),
        'sender_contact'=>array("require","en"),
        'sender_postcode'=>array("require"),
    );
    private  $address_filter=array(
        'sender_name'=>array("require"),
    );
   private $defineAscll = array(43,45);
    protected function _tocheckFiled($filteredData){
        $check_filter = array_merge($this->details_filter,$this->address_filter);
        $pattern = '/[^\x00-\x80]/';
        $isError ="";
        //validate
        foreach($filteredData as $key=>$val){
            foreach ($check_filter[$key] as $rule){
                    if($rule=="require"){
                        if($val==""){
                            $isError.=$key."|字段必须要填写;";
                        }
                    }
                    if($rule=="en"){
                        if(preg_match($pattern,$val)){
                              $isError.= $key."|字段必须为英文;";
                        }
                    }
                    if($rule=="num"){
                          $symbol =   substr($val, 0,1);
                          if(!in_array(ord($symbol), $this->defineAscll)&&!is_numeric($symbol)){
                              $isError.= $key."|字段首字母有误;"; 
                          }else{
                              if(!is_numeric($val)){
                                  $isError.= $key."|字段只能为数字或带有指定符号的数字;";
                              }
                          }
                    }
            }
        }
        if(!empty($isError)){
            $this->_critical($isError,400);
        }
    }
    protected function _toSaveData($postData){
        $getId=$this->getRequest()->getParam("id");
        if($getId){
            $address_model =  Mage::getModel("vendorswarehouse/address")->load($getId);
        }else{
            $address_model =  Mage::getModel("vendorswarehouse/address");
        }
      $VendorId  = $this->getRequest()->getParam('vendorid');
      $vendor =Mage::getModel("vendors/vendor")->load($VendorId);
      $VendorId =$vendor->getVendorId();
        $address_model-> setVendorId($vendor->getId());
        $address_model
        ->setSenderName($postData['sender_name'])
        ->setStatus($postData['status']);

        if(!$getId){
            $head = substr($VendorId,0,strpos($VendorId,"S"));
            //设置仓库编码
            $Collection = Mage::getModel('vendorswarehouse/address')->getCollection()
            ->addFieldToFilter("sender_zip_code",array("like"=>$head."%"));

            $Collection->getSelect()
            ->columns(array("sender_zip_code"=>new Zend_Db_Expr('MAX(sender_zip_code)')));
            $maxCode= $Collection->getFirstItem()->getData("sender_zip_code");
            

            if(empty($maxCode)){
                $startnum = strpos($vendor->getVendorId(),"S");
                if($startnum ==false){
                    $this->_critical("仓库编码生成失败",400);
                }
                $laststr = substr($VendorId,$startnum+1);
                $zipCode = ($head . "SW" . $laststr);
            }else{
                $startnum = strpos($maxCode,"W");
                if($startnum ==false){
                    $this->_critical("仓库编码获取失败",400);
                }
                
                $laststr = (int)substr($maxCode,$startnum+1);
                $laststr+=1;
               if(strlen($laststr)!=5){
                   $laststr =  str_pad($laststr,5,"0",STR_PAD_LEFT);
               }
                 $zipCode = ($head . "SW" . $laststr);
            }
            $address_model->setSenderZipCode($zipCode);
          
        }
        //如果是默认地址  则修改vendors 属性仓库编码的值
        if($postData['is_default']==1){
            $vendor->setWarehouseCode($address_model->getSenderZipCode());
        
            $vendor->save();
        }
        
        try {
            Mage::log("仓库数据");
            Mage::log($address_model->getData());
            $address_model->save();
        } catch (Exception $e) {
            $this->_critical("仓库数据保存失败,请在试一次",400);
        }
   
        $addressId =$address_model->getEntityId();
        if(!$addressId) {
            $this->_critical("仓库地址信息保存失败",400);
        }
        if($getId){
            $details_model =  Mage::getModel("vendorswarehouse/details")->load($getId,"wid");
            $details_model->setUpdateTime(now());
        }else{
            $details_model = Mage::getModel("vendorswarehouse/details");
        }
        foreach($this->details_filter as $attr => $v){
            $details_model->setData($attr,$postData[$attr]);
        }
        $details_model->setWid($addressId);
        $details_model->save();
    }
     

}